﻿///<reference path="../../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo.format.js" />
///<reference path="kooboo.form.js" >

var brand_data = [];
$(function () {
    $("#ChooseBrand").click(function () {
        $("#Brand_Container").dialog("open");
    });

    //close
    $("#close_categories_dialog").click(function () {
        $("input[name='BrandId']").each(function (index, obj) {
            if ($(obj).attr("checked")) {
                $("#BrandId").val($(obj).attr("value"));
                $("#selectedBrandName").html($(obj).next("label").html());
                return;
            }
        });
        $("#Brand_Container").dialog("close");
    });

    $(".prev").click(function () {
        $("#next").val("prev");
    });

    $(".finish").click(function () {
        $("#next").val("finish");
    });

    $(".prev-action-link").click(function (e) {
        e.preventDefault();

        $("#next").val("prev");
        $("form").submit();

        return false;
    });
});

$(document).ready(function () {
    getBrand(1);
    //initial dialog
    $("#Brand_Container").dialog({ autoOpen: false,
        width: 500,
        resize: 'auto',
        modal: true
    });

    $("form").submit(function () {
        $(this).ajaxPost({
            success: function (action) {
                window.location.href = action;
            }
        });
        return false;
    });
});

function getBrand(pageIndex) {
    $.get(
        search_brand_url + "/" + pageIndex + "?BrandId=" + $("#BrandId").val() + "&r=" + Math.random(),
        function (data, status, xhr) {
            var html = "<table><tr><th>Name</th></tr>";
            if (data && data.Items) {
                for (var i in data.Items) {
                    html += rendBrandItem(data.Items[i]);
                }
                html += "</table>";
                $("#brand_table").html(html);

                $("#brand_paging").html(initial_ReviewPager(data.PageIndex, data.PageSize, data.Total));

                initialsingleCheck();
            }
            else {
                alert(data);
            }
        },
        "json");
}

function rendBrandItem(data) {
    return $.format('<tr>'
            + '<td><input type="checkbox" id="chk_{0}" name="BrandId" value="{0}" {1}/>'
            + '<label for="chk_{0}">{2}</label>'
            + '</td>'
            + '</tr>', data.Id, (data.Selected ? "checked" : ""), data.Name);
}

function initial_ReviewPager(cPageIndex, pageSize, total) {
    var html = " <div class='pager'>";
    if (cPageIndex > 1) {
        html += "<a href='javascript:void(0);' onclick='javascript:getBrand(" + (cPageIndex - 1) + ");'> < </a>";
    }
    else {
        html += "<a class='disabled'> < </a>";
    }
    var totalPageNumber = parseInt(total / pageSize) + (total % pageSize > 0 ? 1 : 0);
    for (var i = 1; i <= totalPageNumber; i++) {
        if (i == cPageIndex) {
            html += "<a class='current'>" + i + "</a>";
        } else {
            html += "<a href='javascript:void(0)' onclick='javascript:getBrand(" + i + ");'>" + i + "</a>";
        }
    }
    if (cPageIndex < totalPageNumber) {
        html += "<a href='javascript:void(0);' onclick='javascript:getBrand(" + (cPageIndex + 1) + ");'> > </a>";
    } else {
        html += "<a class='disabled'> > </a>";
    }
    html += "</div>";
    return html;
}

function initialsingleCheck() {
    $("input[name='BrandId']").each(function (index, obj) {
        $(obj).bind("change", function () {
            $("input[name='BrandId']").each(function (i, o) {
                $(o).attr("checked", false);
            });
            $(obj).attr("checked", true);
        });
    });
}

